package com.wf.course.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.wf.course.web.Constants;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

import com.utils.page.PageBean;
import com.wf.course.dao.ICourseCategoryMapper;
import com.wf.course.model.ad.CourseCategory;
import com.wf.course.service.ICourseCategoryService;
import com.wf.course.web.OSSUtil;

@Service
@Transactional
public class ICourseCategoryServiceImpl implements ICourseCategoryService {

	@Autowired
	private ICourseCategoryMapper mapper;
	
	@Override
	public List<CourseCategory> getCourseCategoryAllList() {
		// TODO Auto-generated method stub
		return mapper.getCourseCategoryAllList();
	}
	
	@Override
	public void getCourseCategoryList(PageBean<CourseCategory> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(mapper.getCourseCategoryList(pageBean));
		pageBean.setTotalCount(mapper.getCourseCategoryCount(pageBean));
	}

	@Override
	public Integer getCourseCategoryCount(PageBean<CourseCategory> pageBean) {
		// TODO Auto-generated method stub
		return mapper.getCourseCategoryCount(pageBean);
	}

	@Override
	public void saveCourseCategory(CourseCategory courseCategory, MultipartFile multipartFile) {
		// TODO Auto-generated method stub
		
		if (courseCategory.getId() == null) {
			//新增
			if (!multipartFile.isEmpty()) {
				String imgUrl = "";
				try{
					imgUrl = OSSUtil.addFile(multipartFile,"coursecategory",Constants.IGM_BUCKET);
				}catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				courseCategory.setIcon(imgUrl);
			}
			this.mapper.saveCourseCategory(courseCategory);
		}else {
			//修改
			if (multipartFile.getSize() != 0) {
				CourseCategory cc = mapper.getCourseCategory(courseCategory.getId()); 
				try {
					if (StringUtils.isNotBlank(cc.getIcon())) {
						//删除原来图片
						OSSUtil.delete(cc.getIcon(), Constants.IGM_BUCKET);
					}
					
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				//重新上传新图片
				String imgUrl = "";
				try {
					imgUrl = OSSUtil.addFile(multipartFile, "coursecategory", Constants.IGM_BUCKET);
					
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				courseCategory.setIcon(imgUrl);
			}
			mapper.updateCourseCategory(courseCategory);
		}
	}

	@Override
	public Integer updateCourseCategory(CourseCategory courseCategory) {
		// TODO Auto-generated method stub
		return mapper.updateCourseCategory(courseCategory);
	}

	@Override
	public CourseCategory getCourseCategory(Integer id) {
		// TODO Auto-generated method stub
		return mapper.getCourseCategory(id);
	}

	@Override
	public void deleteCourseCategory(Integer id) {
		// TODO Auto-generated method stub
		mapper.deleteCourseCategory(id);
	}

	@Override
	public List<CourseCategory> getParentCourseCategory(Integer pid) {
		// TODO Auto-generated method stub
		return mapper.getParentCourseCategory(pid);
	}
}
